package com.svc;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException; 
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
 
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.spi.ContainerSystem;
import org.apache.openejb.util.Debug;
import org.hibernate.ejb.EntityManagerFactoryImpl;
import org.hibernate.ejb.EntityManagerImpl;
import org.hibernate.ejb.HibernatePersistence;
import org.hibernate.ejb.AbstractEntityManagerImpl;
import org.hibernate.persister.entity.SingleTableEntityPersister;

import bsh.EvalError;
import bsh.Interpreter;

import com.base.service.SVC_BASELocal;
import com.dpmc.service.SVC_SHGLRemote;
import com.organization.service.SVC_ORGLocal;
 
 

public class OpenEJBsvc {
	
	public static InitialContext tomee_InitialContext() throws NamingException{
		Properties properties = new Properties();
		 
	    properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");   
        properties.setProperty(Context.PROVIDER_URL, "http://127.0.0.1:8080/tomee/ejb"); 
        return new InitialContext(properties);   
	}
	
	public static InitialContext openEJB_InitialContext() throws NamingException{
		  Properties properties = new Properties();   
	        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");   
	       // properties.setProperty("openejb.embedded.remotable", "true");    
	        properties.put("db_dpmc", "new://Resource?type=DataSource");
	        properties.put("db_dpmc.JdbcDriver", "com.mysql.jdbc.Driver");
	        properties.put("db_dpmc.JdbcUrl", "jdbc:mysql://127.0.0.1:3306/db_dpmc?useUnicode=true&amp;amp;characterEncoding=utf-8");
	        properties.put("db_dpmc.UserName", "root");
	        properties.put("db_dpmc.Password", "1111");
	        properties.put("db_dpmc.JtaManaged", "true");
	        
	        properties.put("db_dpmc_unmanaged", "new://Resource?type=DataSource");
	        properties.put("db_dpmc_unmanaged.JdbcDriver", "com.mysql.jdbc.Driver");
	        properties.put("db_dpmc_unmanaged.JdbcUrl", "jdbc:mysql://127.0.0.1:3306/db_dpmc?useUnicode=true&amp;amp;characterEncoding=utf-8");
	        properties.put("db_dpmc_unmanaged.UserName", "root");
	        properties.put("db_dpmc_unmanaged.Password", "1111");
	        properties.put("db_dpmc_unmanaged.JtaManaged", "false"); 
	       
	           
	        return new InitialContext(properties);   
	}
	public static void mainxxx (String[] args) throws NamingException { 
		 InitialContext ctx = openEJB_InitialContext();   
		 ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
		 Context ctx1 = containerSystem.getJNDIContext();
		 System.out.println( ctx );
		 System.out.println( ctx1 );
	}
	
	public static void main (String[] args) throws NamingException { 
		
        //   
        @SuppressWarnings("unused")   
        InitialContext ctx = openEJB_InitialContext();   
       // System.out.println(ctx.lookup("java:/EntityManagers/res-unit"));
        Context context = (Context) ctx.lookup("java:openejb/PersistenceUnit");  
  
        Map<String,Object> map = Debug.contextToMap(context); 
        for (String key : map.keySet()) {
        	System.out.println( key ); 
            System.out.println(map.get(key)); 
            EntityManagerFactoryImpl emf = (EntityManagerFactoryImpl) map.get(key);
           
            Map metadata = emf.getSessionFactory().getAllClassMetadata();
            for(Object k :metadata.keySet()){
            	 System.out.println( k ); 
            	 
            	 SingleTableEntityPersister step =  (SingleTableEntityPersister) metadata.get(k);
            	 System.out.println( step.getClassMetadata().getEntityName());
            	for(Object pn: step.getPropertyNames()){
            		// System.out.println( pn);
            	}
            } 
        } 
        System.out.println( "--------------"); 
        System.out.println( Debug.contextToMap(ctx)); 
        System.out.println( "--------------"); 
        NamingEnumeration<Binding> namingEnumeration = ctx.listBindings("");
        while (namingEnumeration.hasMoreElements()) {
         
            Binding binding = namingEnumeration.nextElement();
            String name = binding.getName();
            System.out.println( name);
            System.out.println( binding.getClassName());
        }
       
       // EntityManagerImpl em  ; 
       // EntityManagerFactoryImpl emfi = null;
      //  EntityManagerFactory emf= Persistence.createEntityManagerFactory("java:/res-unit");
 
	
       
          
	}
	
	
	public static void main11(String[] args) throws NamingException { 
	      
        //   
        @SuppressWarnings("unused")   
        InitialContext ctx = openEJB_InitialContext();   
       
        SVC_ORGLocal svc = (SVC_ORGLocal) ctx.lookup("SVC_ORGLocal");
       
        
        System.out.println("_____________-----="+svc.getBelongToOrgs("4028807c340c4f2301340c5f8b590026"));
	}

	/**
	 * @param args
	 * @throws NamingException 
	 */
	public static void main2(String[] args) throws NamingException { 
      
        //   
        @SuppressWarnings("unused")   
        InitialContext ctx = openEJB_InitialContext();   
       
        SVC_ORGLocal svc = (SVC_ORGLocal) ctx.lookup("SVC_ORGLocal");
       
        HashMap param = new HashMap();  
         param.put("INFO_ID", "40288071326ca44801326ca448a60000" ); 
         param.put("INFO_TYPE", "TB_QDSGL_JBXX" );  
        System.out.println("_____________-----="+svc.queryForResultSize("Q_SHGL_SHJBXX.queryForResultSize",param) );
	}
	
	public static void main1(String[] args) throws EvalError, NamingException {
		  InitialContext ctx = openEJB_InitialContext();    
	      SVC_ORGLocal svc = (SVC_ORGLocal) ctx.lookup("SVC_ORGLocal");
		Interpreter interpreter = new Interpreter(); // 构造一个解释器 
		interpreter.set("svc", svc);
	//	interpreter.eval("com.core.service.BaseService;");
		interpreter.eval(" System.out.println( svc.getResultList(\"Q_ORG_PARTTYPE.GET_TB_ORG_PARTTYPE_LIST\"));");
		
		interpreter.eval("import com.common.Arith;");
	     interpreter.eval("double value = 20;" +
	      		"double n1=0.2;" +
	      		"double n2=1.2;" +
	      		"value =  Arith.sub(value-n1,n2); "); // 对一段脚本求值，并得到结果
	      Double yj = (Double) interpreter.get("value");
	      System.out.println(yj);

	}
}
